package com.infoDiffusion.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.infoDiffusion.bean.UserBean;
import com.infoDiffusion.dao.AdminDao;
@Service
@Transactional(propagation = Propagation.REQUIRED)
public class AdminService {
	@Autowired
	private AdminDao adminDao;

	public UserBean login(UserBean userBean) {
		String hql = "from UserBean u where  u.username=? and u.password=? and u.power=?";
		return (UserBean) adminDao.getByCondition(hql,
				userBean.getUsername(), userBean.getPassword(),userBean.getPower());
	}

	public boolean add(UserBean userBean) {
		return adminDao.add(userBean) ;
	}

	public boolean delete(UserBean user) {
		adminDao.delete(user);
		return true;
	}
	public boolean updateById(int id){
		String hql = "update UserBean set password=? where id=?";
		return adminDao.updateById(hql, id);
	}

	public boolean  updatePass(UserBean userBean) {
		String hql ="update UserBean u set u.password=?where u.id=?";
		return  adminDao.update(hql,userBean.getPassword(),userBean.getId());
	}
	
	public List list(UserBean userBean) {
		String hql = "from UserBean";
		return adminDao.getList(hql);
	}

	public int getCount() {
		String hql = "select count(id) from UserBean";
		return adminDao.getCount(hql);
	}

	public List<UserBean> getByPage(int start, int length) {
		String hql = "from UserBean";
		return adminDao.getListForPage(hql, start, length);
	}

	public UserBean getById(int id) {
		return (UserBean) adminDao.getById(UserBean.class, id);
	}

	public boolean  update(UserBean userBean ) {
		//String hql ="update UserBean u set u.username=?,u.password=?,power=? where id=?";
		return  adminDao.update(userBean);
	}
	
	public UserBean getByUsername(String username) {
		return (UserBean) adminDao.getByCondition("from UserBean where username=?", username);
	}
	
}
